﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;

namespace System
{
    public static class TypeExt
    {
        /// <summary>
        /// Checks if given type inherits typeFullName
        /// </summary>
        /// <param name="type"></param>
        /// <param name="typeFullName"></param>
        /// <param name="ignoreCase"></param>
        /// <param name="checkInterfaces">If true interfaces are also checked</param>
        /// <returns></returns>
        public static bool IsAccesibleForm(this Type type, string typeFullName, bool ignoreCase = true, bool checkInterfaces = true)
        {
            IComparer comparer = null;
            if(ignoreCase)
                comparer = StringComparer.OrdinalIgnoreCase;
            if (type.FullName.IsTheSame(typeFullName, comparer))
                return true;
            if (checkInterfaces)
            {
                Type[] interfaces = type.GetInterfaces();
                if (interfaces != null && interfaces.Length > 0)
                {
                    foreach (Type i in interfaces)
                        if (i.FullName.IsTheSame(typeFullName, comparer))
                            return true;
                }
            }
            bool result = false;
            while (type != null && type != typeof(object))
            {
                type = type.BaseType;
                result = type.IsAccesibleForm(typeFullName, ignoreCase, false);
                if (result)
                    return true;
            }
            return false;
        }

        
    }
}
